﻿using System;
using System.Windows.Media;

namespace NyanBankIntfTool
{
    public class LogText : EventArgs
    {
        public LogText()
        {
            Text = "";
            Brush = Brushes.Black;
        }

        public LogText(string text)
        {
            Text = text;
            Brush = Brushes.Black;
        }

        public LogText(string text, Brush brush)
        {
            Text = text;
            Brush = brush;
        }

        public string Text { get; set; }
        public Brush Brush { get; set; }
    }

    static class Logger
    {
        public static event EventHandler<LogText> LogEvent;
        private static readonly string[] Levels = new string[] {"错误", "提示", "信息", "调试"};

        private static void Log(LogText logText)
        {
            LogEvent?.Invoke(null, logText);
#if !DEBUG
            //System.Threading.Thread.Sleep(100);
#endif
        }

        public static void Log(string text, LogLevel level = LogLevel.Info)
        {
            if (level != LogLevel.Output)
            {
                var logText = new LogText($"{DateTime.Now}: [{Levels[(int) level]}]{text}\n");
                switch (level)
                {
                    case LogLevel.Error:
                        logText.Brush = Brushes.Red;
                        break;
                    case LogLevel.Warning:
                        logText.Brush = Brushes.Blue;
                        break;
                    default:
                        logText.Brush = Brushes.Black;
                        break;
                }

                Log(logText);
            }
            else
            {
                var logText = new LogText($"{text}\n");
                Log(logText);
            }
        }
    }

    enum LogLevel
    {
        Output = -1,
        Error = 0,
        Warning,
        Info,
        Debug
    };
}